/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import Class.RepeatPaginator;
import DAO.AccountDAO;
import DAO.CompanyDAO;
import DAO.PaymentDAO;
import Models.Account;
import Models.Company;
import Models.Payment;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Cham to tet
 */
@ManagedBean
@RequestScoped
public class CompanyController {

    RepeatPaginator paginator;
    String CitySearch, Keyword, FindWhere;

    public RepeatPaginator getPaginator() {
        return paginator;
    }

    public String getCitySearch() {
        return CitySearch;
    }

    public void setCitySearch(String CitySearch) {
        this.CitySearch = CitySearch;
    }

    public String getFindWhere() {
        return FindWhere;
    }

    public void setFindWhere(String FindWhere) {
        this.FindWhere = FindWhere;
    }

    public String getKeyword() {
        return Keyword;
    }

    public void setKeyword(String Keyword) {
        this.Keyword = Keyword;
    }

    /**
     * Creates a new instance of CompanyController
     */
    public CompanyController() {
        try {
            this.paginator = new RepeatPaginator(CompanyDAO.getCompanyByKeyword("", "Name"));
        } catch (Exception ex) {
            Logger.getLogger(CompanyController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void advanceSearch() {
        try {
            this.paginator = new RepeatPaginator(CompanyDAO.getCompanyByKeyword(Keyword, FindWhere));
        } catch (Exception ex) {
            Logger.getLogger(CompanyController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void searchByCity() {
        try {
            try {
                this.paginator = new RepeatPaginator(CompanyDAO.getCompanyByKeyword(CitySearch, "Address"));
            } catch (Exception ex) {
                Logger.getLogger(CompanyController.class.getName()).log(Level.SEVERE, null, ex);
            }
            FacesContext.getCurrentInstance().getExternalContext().redirect("Listing.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(CompanyController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String processRegister() {
        Company company = (Company) (FacesContext.getCurrentInstance().getExternalContext()).getRequestMap().get("companyBean");
        Account acc = (Account) (FacesContext.getCurrentInstance().getExternalContext()).getRequestMap().get("accountBean");
        Payment payment = (Payment) (FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("paymentBean"));

        new CompanyDAO().addListing(company);

        acc.setCompanyID(new CompanyDAO().getCurrentID());
        acc.setDriversID(0);
        new AccountDAO().addAccount(acc);

        payment.setCompanyID(new CompanyDAO().getCurrentID());
        payment.setDriversID(0);
        payment.setAdvertiseID(0);

        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) e.getSession(true);
        int money;
        if (payment.getExtensionType().equalsIgnoreCase("Monthly")) {
            money = payment.getAmount() * 15;
        } else {
            money = payment.getAmount() * 40;
        }
        payment.setMoney(money);
        session.setAttribute("Money", money);
        new PaymentDAO().addPayment(payment);

        if (payment.getPaymentType().equals("Cash")) {
            return "PayByCash";
        } else {
            return "PayByBank";
        }

    }
    List<Company> listCom = new ArrayList<Company>();

    public List<Company> getListCom() throws Exception {
        return listCom = CompanyDAO.getAllCompany();
    }

    public void setListCom(List<Company> listCom) {
        this.listCom = listCom;
    }

    public void selectedID(int selectedID) {
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) e.getSession(true);
        session.setAttribute("selectedID", selectedID);
    }

    public void deleteCompany() {
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) e.getSession(true);
        int id = Integer.parseInt(session.getAttribute("selectedID").toString());
        new CompanyDAO().deleteCompany(id);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Information: ", "1 Company has been deleted !"));
    }

    public void editSubmit() throws Exception {
        Company com = (Company) (FacesContext.getCurrentInstance().getExternalContext().
                getRequestMap().get("companyBean"));
        CompanyDAO.editCompany(com);
    }

    public void editCompanyByID() throws Exception {
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) e.getSession(true);
        Account a = (Account) session.getAttribute("account");
        
        Company company = (Company) (FacesContext.getCurrentInstance().getExternalContext()).getRequestMap().get("companyBean");
        company = CompanyDAO.getCompanyByID(a.getCompanyID());
    }
}
